package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.constant.NetConstant;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;
import com.ycsj.goldmedalnewconcept.R;

public class SettingInfoActivity extends BaseJpStarActivity implements OnClickListener{

	private TextView tv_save;
	private TextView tv_title_main;
	private TextView tv_main_back;
	private EditText et_edit;
	private int id;
	private String nickname;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settinginfo);
		id = getIntent().getIntExtra("ID", 0);
		
		SystemBarTintManager tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setStatusBarTintResource(R.color.base_color);
		
		init();
	}

	private void init() {
		tv_save = (TextView) findViewById(R.id.tv_save);
		tv_title_main = (TextView) findViewById(R.id.tv_title_main);
		tv_main_back = (TextView) findViewById(R.id.tv_main_back);
		 et_edit = (EditText) findViewById(R.id.et_edit);
		 
		tv_save.setOnClickListener(this);
		tv_title_main.setOnClickListener(this);
		tv_main_back.setOnClickListener(this);
		initView();
	}
	

	private void initView() {
		if(id==1){
			et_edit.setHint("请输入姓名");
		}else if(id==2){
			et_edit.setHint("请输入联系方式");
		}else if(id==3){
			et_edit.setHint("请输入学校");
		}else if(id==4){
			et_edit.setHint("请输入年级");
		}else if(id==5){
			et_edit.setHint("请输入班级");
		}else if(id==6){
			et_edit.setHint("请输入学校");
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_main_back:
			finish();
			break;
		case R.id.tv_save:
			et_string = et_edit.getText().toString();
			if(id==1&&!"".equals(et_string)){
				
			//姓名	
			String pathNickname=NetConstant.ACCOUNTINFO+ "/UpdateStudentInfo.aspx?account="+ SPUtils.getString(SettingInfoActivity.this,
					ConfigConstant.USERNAME)+ "&u_c=2&usertoken="+ SPUtils.getString(SettingInfoActivity.this,
					ConfigConstant.USERTOKEN)+"&type=1&value="+et_string;
			
			
			
			OkHttpClient mOkHttpClient = new OkHttpClient();
			// 创建一个Request
			final Request request = new Request.Builder().url(pathNickname).build();
			// new call
			Call call = mOkHttpClient.newCall(request);
			call.enqueue(callBack1);
			}else if(id==2&&!"".equals(et_string)){
				
				
				
				//联系方式
				String pathTel=NetConstant.ACCOUNTINFO+ "/UpdateStudentInfo.aspx?account="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERNAME)+ "&u_c=2&usertoken="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERTOKEN)+"&type=4&value="+et_string;
				
				
				
				
				OkHttpClient mOkHttpClient = new OkHttpClient();
				// 创建一个Request
				final Request request = new Request.Builder().url(pathTel).build();
				// new call
				Call call = mOkHttpClient.newCall(request);
				call.enqueue(callBack1);
			}else if(id==3&&!"".equals(et_string)){
				
				//学校
				String pathSchool=NetConstant.ACCOUNTINFO+ "/UpdateStudentInfo.aspx?account="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERNAME)+ "&u_c=2&usertoken="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERTOKEN)+"&type=5&value="+et_string;
				
				
				OkHttpClient mOkHttpClient = new OkHttpClient();
				// 创建一个Request
				final Request request = new Request.Builder().url(pathSchool).build();
				// new call
				Call call = mOkHttpClient.newCall(request);
				call.enqueue(callBack1);
			}else if(id==4&&!"".equals(et_string)){
				String pathGrade=NetConstant.ACCOUNTINFO+ "/UpdateStudentInfo.aspx?account="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERNAME)+ "&u_c=2&usertoken="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERTOKEN)+"&type=6&value="+et_string;
				OkHttpClient mOkHttpClient = new OkHttpClient();
				// 创建一个Request
				final Request request = new Request.Builder().url(pathGrade).build();
				// new call
				Call call = mOkHttpClient.newCall(request);
				call.enqueue(callBack1);
			}else if(id==5&&!"".equals(et_string)){
				String pathCLASS=NetConstant.ACCOUNTINFO+ "/UpdateStudentInfo.aspx?account="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERNAME)+ "&u_c=2&usertoken="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERTOKEN)+"&type=7&value="+et_string;
				OkHttpClient mOkHttpClient = new OkHttpClient();
				// 创建一个Request
				final Request request = new Request.Builder().url(pathCLASS).build();
				// new call
				Call call = mOkHttpClient.newCall(request);
				call.enqueue(callBack1);
			}else if(id==6&&!"".equals(et_string)){
				String pathSchool=NetConstant.ACCOUNTINFO+ "/UpdateStudentInfo.aspx?account="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERNAME)+ "&u_c=2&usertoken="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERTOKEN)+"&type=5&value="+et_string;
				OkHttpClient mOkHttpClient = new OkHttpClient();
				// 创建一个Request
				final Request request = new Request.Builder().url(pathSchool).build();
				// new call
				Call call = mOkHttpClient.newCall(request);
				call.enqueue(callBack1);
			}
			SPUtils.put(SettingInfoActivity.this, "RongIMNeedRefresh", true);
			finish();
			break;
		default:
			break;
		}
	}
	
	Callback callBack1=new Callback() {
		
		@Override
		public void onResponse(Response arg0) throws IOException {
			// TODO Auto-generated method stub
			String json = arg0.body().string();
			Log.e("test", json);
		}
		
		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub
			
		}
	};
	private String et_string;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if((keyCode == KeyEvent.KEYCODE_BACK)){
			et_string = et_edit.getText().toString();
			if(id==1&&!"".equals(et_string)){
			String pathNickname=NetConstant.ACCOUNTINFO+ "/UpdateStudentInfo.aspx?account="+ SPUtils.getString(SettingInfoActivity.this,
					ConfigConstant.USERNAME)+ "&u_c=2&usertoken="+ SPUtils.getString(SettingInfoActivity.this,
					ConfigConstant.USERTOKEN)+"&type=1&value="+et_string;
			OkHttpClient mOkHttpClient = new OkHttpClient();
			// 创建一个Request
			final Request request = new Request.Builder().url(pathNickname).build();
			// new call
			Call call = mOkHttpClient.newCall(request);
			call.enqueue(callBack1);
			}else if(id==2&&!"".equals(et_string)){
				String pathTel=NetConstant.ACCOUNTINFO+ "/UpdateStudentInfo.aspx?account="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERNAME)+ "&u_c=2&usertoken="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERTOKEN)+"&type=4&value="+et_string;
				OkHttpClient mOkHttpClient = new OkHttpClient();
				// 创建一个Request
				final Request request = new Request.Builder().url(pathTel).build();
				// new call
				Call call = mOkHttpClient.newCall(request);
				call.enqueue(callBack1);
			}else if(id==3&&!"".equals(et_string)){
				String pathSchool=NetConstant.ACCOUNTINFO+ "/UpdateStudentInfo.aspx?account="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERNAME)+ "&u_c=2&usertoken="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERTOKEN)+"&type=5&value="+et_string;
				OkHttpClient mOkHttpClient = new OkHttpClient();
				// 创建一个Request
				final Request request = new Request.Builder().url(pathSchool).build();
				// new call
				Call call = mOkHttpClient.newCall(request);
				call.enqueue(callBack1);
			}else if(id==4&&!"".equals(et_string)){
				String pathGrade=NetConstant.ACCOUNTINFO+ "/UpdateStudentInfo.aspx?account="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERNAME)+ "&u_c=2&usertoken="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERTOKEN)+"&type=6&value="+et_string;
				OkHttpClient mOkHttpClient = new OkHttpClient();
				// 创建一个Request
				final Request request = new Request.Builder().url(pathGrade).build();
				// new call
				Call call = mOkHttpClient.newCall(request);
				call.enqueue(callBack1);
			}else if(id==5&&!"".equals(et_string)){
				String pathCLASS=NetConstant.ACCOUNTINFO+ "/UpdateStudentInfo.aspx?account="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERNAME)+ "&u_c=2&usertoken="+ SPUtils.getString(SettingInfoActivity.this,
						ConfigConstant.USERTOKEN)+"&type=7&value="+et_string;
				OkHttpClient mOkHttpClient = new OkHttpClient();
				// 创建一个Request
				final Request request = new Request.Builder().url(pathCLASS).build();
				// new call
				Call call = mOkHttpClient.newCall(request);
				call.enqueue(callBack1);
		}
		
		finish();
	}
		return false;
	
	}
	
}
